package team.seekers.hula.teamwork.teamTodo.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.hula.common.entity.result.ApiExceptionEnum;
import team.seekers.hula.common.entity.result.ApiException;
import team.seekers.hula.teamwork.team.team.entity.dao.TeamDO;
import team.seekers.hula.teamwork.team.team.service.TeamService;
import team.seekers.hula.teamwork.teamTodo.entity.dao.TeamTodoDO;
import team.seekers.hula.teamwork.teamTodo.entity.dto.*;
import team.seekers.hula.teamwork.teamTodo.entity.vo.TeamTodoListVO;
import team.seekers.hula.teamwork.teamTodo.entity.vo.TeamTodoManageInfoVO;
import team.seekers.hula.teamwork.teamTodo.mapper.TeamTodoMapper;
import team.seekers.hula.teamwork.todo.service.TodoService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 团队待办 服务实现
 * @author hbc
 * @since 2023-01-05
 */

@Service
public class TeamTodoService extends ServiceImpl<TeamTodoMapper, TeamTodoDO>{

    @Resource
    @Lazy
    private TodoService todoService;

    @Resource
    private TeamService teamService;


    /**
     * 获取团队任务详情
     */
    public TeamTodoManageInfoVO info(String id) {
        TeamTodoManageInfoVO teamTodoInfoVO = new TeamTodoManageInfoVO();

        TeamTodoDO teamTodoDO = getById(id);
        BeanUtils.copyProperties(teamTodoDO,teamTodoInfoVO);

        // 设置负责人信息
        teamTodoInfoVO.setMonitor(todoService.getMonitorTodoInfo(teamTodoDO.getId(),teamTodoDO.getUserId()));

        // 设置共创者信息
        teamTodoInfoVO.setParticipants(todoService.getParticipantTodoInfoList(teamTodoDO.getId()));

        return teamTodoInfoVO;
    }


    /**
     * 新增团队任务
     */
    @Transactional(rollbackFor = Exception.class)
    public TeamTodoManageInfoVO add(TeamTodoAddDTO teamTodoAddDTO) {
        // 新建团队待办
        TeamTodoDO teamTodoDO = new TeamTodoDO();
        BeanUtils.copyProperties(teamTodoAddDTO, teamTodoDO);
        teamTodoDO.setParticipants(JSON.toJSONString(teamTodoAddDTO.getParticipants()));
        teamTodoDO.setUserId(teamTodoAddDTO.getMonitor().getUserInfo().getId());
        save(teamTodoDO);

        // 更新团队的待办数量
        TeamDO teamDO = teamService.getById(teamTodoAddDTO.getTeamId());
        teamDO.setTodoCount(teamDO.getTodoCount() + 1);
        teamService.updateById(teamDO);

        // 插入负责人待办
        // 填入团队待办信息
        TeamTodoParticipantAddDTO monitor = teamTodoAddDTO.getMonitor();
        monitor.setTeamTodoId(teamTodoDO.getId());
        monitor.setTeamTodoTitle(teamTodoDO.getTitle());
        todoService.addTeamMonitorTodo(teamTodoDO,teamTodoAddDTO.getMonitor());

        // 插入参与人待办
        // 填入团队待办信息
        for (TeamTodoParticipantAddDTO participant : teamTodoAddDTO.getParticipants()) {
            participant.setTeamTodoId(teamTodoDO.getId());
            participant.setTeamTodoTitle(teamTodoDO.getTitle());
        }
        todoService.addTeamParticipantTodos(teamTodoAddDTO.getParticipants());


        return info(teamTodoDO.getId());
    }

    /**
     * 编辑团队任务
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(TeamTodoUpdateDTO teamTodoUpdateDTO) {
        TeamTodoDO teamTodoDO = getById(teamTodoUpdateDTO.getId());
        BeanUtils.copyProperties(teamTodoUpdateDTO, teamTodoDO);
        updateById(teamTodoDO);


        // 修改队员团队待办
        TodoTeamUpdateDTO todoTeamUpdateDTO = new TodoTeamUpdateDTO();
        BeanUtils.copyProperties(teamTodoDO, todoTeamUpdateDTO);
        todoTeamUpdateDTO.setTeamTodoId(teamTodoDO.getId());
        todoService.updateTeamParticipantTodos(teamTodoDO);
    }

    /**
     * 新建团队待办共创者任务
     */
    public String addParticipant(TeamTodoParticipantAddDTO participantAddDTO) {
        return todoService.addTeamParticipantTodo(participantAddDTO);
    }

    /**
     * 编辑团队待办共创者任务
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateParticipant(TeamTodoParticipantUpdateDTO participantInfoDTO) {
        todoService.updateTeamParticipantTodo(participantInfoDTO);
    }


    /**
     * 删除团队任务
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        TeamTodoDO teamTodoDO = getById(id);

        // 修改团队数据
        TeamDO teamDO = teamService.getById(teamTodoDO.getTeamId());
        teamDO.setTodoCount(teamDO.getTodoCount() - 1);
        teamService.updateById(teamDO);

        // 删除队员团队待办
        todoService.deleteTeam(id);

        removeById(id);
    }

    /**
     * 根据teamId查询所有团队待办
     */
    public List<TeamTodoListVO> teamTodoList(String teamId) {
        List<TeamTodoListVO> teamTodoListVOS = new ArrayList<>();
        QueryWrapper<TeamTodoDO> wrapper = new QueryWrapper<>();
        wrapper.eq("team_id", teamId);

        // 封装团体待办信息
        TeamTodoListVO teamTodoListVO;
        List<TeamTodoDO> teamTodoDOList = baseMapper.selectList(wrapper);
        if(teamTodoDOList == null){
            throw new ApiException(ApiExceptionEnum.DB_FAIL);
        }
        for (TeamTodoDO teamTodoDO : teamTodoDOList) {
            teamTodoListVO = new TeamTodoListVO();
            BeanUtils.copyProperties(teamTodoDO, teamTodoListVO);
            teamTodoListVOS.add(teamTodoListVO);
        }
        return teamTodoListVOS;
    }

    /**
     * 根据teamId查询所有团队待办id
     */
    public List<String> teamTodoIdList(String teamId) {
        List<TeamTodoDO> teamTodoDOS = lambdaQuery().select(TeamTodoDO::getId).eq(TeamTodoDO::getTeamId, teamId).list();
        ArrayList<String> ids = new ArrayList<>();

        for (TeamTodoDO teamTodoDO : teamTodoDOS) {
            ids.add(teamTodoDO.getId());
        }
        return ids;
    }

    /**
     * 根据teamId查询所有未完成的团队待办
     */
    public List<TeamTodoListVO> listUnFinished(String teamId) {
        List<TeamTodoDO> teamTodoDOList = lambdaQuery().eq(TeamTodoDO::getTeamId,teamId).list();

        // 封装团体待办信息
        List<TeamTodoListVO> teamTodoListVOS = new ArrayList<>();
        for (TeamTodoDO teamTodoDO : teamTodoDOList) {
            TeamTodoListVO teamTodoListVO = new TeamTodoListVO();
            BeanUtils.copyProperties(teamTodoDO, teamTodoListVO);
            teamTodoListVOS.add(teamTodoListVO);
        }
        return teamTodoListVOS;
    }

    /**
     * 根据团队id删除团队待办
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void removeByTeamId(String teamId) {
        List<TeamTodoDO> teamTodoDOList = lambdaQuery().eq(TeamTodoDO::getTeamId,teamId).list();

        // 根据团队待办id删除个人团队待办
        ArrayList<String> teamTodoIds = new ArrayList<>();
        for (TeamTodoDO teamTodoDO : teamTodoDOList) {
            teamTodoIds.add(teamTodoDO.getId());
        }
        if(teamTodoIds.size() > 0) {
            todoService.removeByTeamTodoIds(teamTodoIds);
        }

        lambdaUpdate().eq(TeamTodoDO::getTeamId,teamId).remove();
    }



}
